<template>
	<div class="app-container">
		<el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
			label-width="68px">
			<el-form-item label="出库编码" prop="outboundCode">
				<el-input v-model="queryParams.outboundCode" placeholder="请输入出库编码" clearable
					@keyup.enter.native="handleQuery" />
			</el-form-item>
			<el-form-item label="出库类型" prop="outboundType">
				<el-select v-model="queryParams.outboundType" placeholder="请选择出库类型" clearable>
					<el-option v-for="dict in dict.type.py_outbound_type" :key="dict.value" :label="dict.label"
						:value="dict.value" />
				</el-select>
			</el-form-item>
			<el-form-item label="仓库名称" prop="warehouseCode">
				<el-input v-model="queryParams.warehouseCode" placeholder="请输入仓库名称" clearable
					@keyup.enter.native="handleQuery" />
			</el-form-item>
			<el-form-item>
				<el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
				<el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
			</el-form-item>
		</el-form>

		<el-row :gutter="10" class="mb8">
			<el-col :span="1.5">
				<el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
					v-hasPermi="['outbound:outbound:add']">新增</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
					v-hasPermi="['outbound:outbound:edit']">修改</el-button>
			</el-col>
			<!-- <el-col :span="1.5">
				<el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple"
					@click="handleDelete" v-hasPermi="['outbound:outbound:remove']">删除</el-button>
			</el-col> -->
			<el-col :span="1.5">
				<el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
					v-hasPermi="['outbound:outbound:export']">导出</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="primary" plain icon="el-icon-user" size="mini" @click="sh"
					v-hasPermi="['outbound:outbound:check']" :disabled="single">
					审核
				</el-button>
			</el-col>
			<!-- <el-col :span="1.5">
				<el-button type="primary" plain icon="el-icon-s-release" size="mini" @click="cx"
					v-hasPermi="['inbound:inbound:revoke']" :disabled="!isWithdrawalEnabled">撤销</el-button>
			</el-col> -->
			<right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
		</el-row>

		<!-- 审核弹出框 -->
		<el-dialog title="审核" :visible.sync="shopen" width="500px">
			<el-form :model="shform">
				<el-form-item label="审核结果" prop="result">
					<el-radio-group v-model="shform.result">
						<el-radio :label="2">通过</el-radio>
						<el-radio :label="1">驳回</el-radio>
					</el-radio-group>
				</el-form-item>
				<el-form-item label="审核意见" prop="comments">
					<el-input type="textarea" v-model="shform.comments" placeholder="请输入"></el-input>
				</el-form-item>
			</el-form>

			<span slot="footer" class="dialog-footer">
				<el-button type="primary" @click="submitReview">确定</el-button>
				<el-button @click="shresetForm">取消</el-button>
			</span>
		</el-dialog>

		<el-table @sort-change="handleSortChange" v-loading="loading" :data="outboundList"
			@selection-change="handleSelectionChange">
			<el-table-column type="selection" width="55" align="center" />
			<el-table-column label="出库Id" align="center" prop="outboundId" sortable fixed />
			<el-table-column label="出库编码" align="center" prop="outboundCode" width="155" fixed />
			<el-table-column label="出库类型" align="center" prop="outboundType" fixed>
				<template slot-scope="scope">
					<dict-tag :options="dict.type.py_outbound_type" :value="scope.row.outboundType" />
				</template>
			</el-table-column>
			<el-table-column label="出库状态" align="center" prop="outboundStatus" sortable fixed>
				<template slot-scope="scope">
					<dict-tag :options="dict.type.py_documents_status" :value="scope.row.outboundStatus" />
				</template>
			</el-table-column>
			</el-table-column>
			<el-table-column label="出库日期" align="center" prop="outboundTime" width="95" fixed>
				<template slot-scope="scope">
					<span>{{ parseTime(scope.row.outboundTime, '{y}-{m}-{d}') }}</span>
				</template>
			</el-table-column>
			<el-table-column label="仓库名称" align="center" prop="warehouseCode" />
			<el-table-column label="关联单号" align="center" prop="relatedNumber" width="155" />
			<el-table-column label="出库目标" align="center" prop="relatedTarget" />
			<el-table-column label="收货人" align="center" prop="consignee" />
			<el-table-column label="联系方式 " align="center" prop="phone" width="110" />
			<el-table-column label="出库货品" align="center" prop="productNames" width="180"
				:show-overflow-tooltip="true" />
			<el-table-column label="出库数量" align="center" prop="reviewerNumber" />
			<el-table-column label="出库金额" align="center" prop="reviewerMoney" />
			<el-table-column label="审核人" align="center" prop="reviewer" />
			<el-table-column label="审核理由" align="center" prop="reviewerReport" />
			<el-table-column label="审核时间" align="center" prop="reviewerTime" width="95">
				<template slot-scope="scope">
					<span>{{ parseTime(scope.row.reviewerTime, '{y}-{m}-{d}') }}</span>
				</template>
			</el-table-column>
			<el-table-column label="创建人" align="center" prop="createBy" />
			<el-table-column label="创建时间" align="center" prop="createTime" width="95">
				<template slot-scope="scope">
					<span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
				</template>
			</el-table-column>
			<el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="155" fixed="right">
				<template slot-scope="scope" v-if="scope.row.outboundStatus != 2">
					<el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
						v-hasPermi="['outbound:outbound:edit']">修改</el-button>
					<el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
						v-hasPermi="['outbound:outbound:remove']">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
			@pagination="getList" />

	</div>
</template>

<script>
	import {
		listOutbound,
		getOutbound,
		delOutbound,
		addOutbound,
		updateOutbound,
		updateStatus
	} from "@/api/outbound/outbound";

	export default {
		name: "Outbound",
		dicts: ['py_documents_status', 'py_outbound_type'],
		data() {
			return {
				// 遮罩层
				loading: true,
				// 选中数组
				ids: [],
				// 非单个禁用
				single: true,
				// 非多个禁用
				multiple: true,
				// 显示搜索条件
				showSearch: true,
				// 总条数
				total: 0,
				// 出库管理表格数据
				outboundList: [],
				// 弹出层标题
				title: "",
				// 是否显示弹出层
				open: false,
				// 查询参数
				queryParams: {
					pageNum: 1,
					pageSize: 10,
					outboundCode: null,
					outboundType: null,
					outboundStatus: null,
					warehouseCode: null,
				},
				// 表单参数
				form: {},
				// 表单校验
				rules: {},
				//审核弹出
				shopen: false,
				shform: {
					result: 2,
					comments: '',
				},
				isClick: true,
			};
		},
		created() {
			this.getList();
		},
		methods: {
			customSort(a, b) {
				// 先根据类型等于1排序
				if (a.outboundStatus != 2 && b.outboundStatus == 2) return -1;
				if (a.outboundStatus == 2 && b.outboundStatus != 2) return 1;

				// 再根据编号排序
				if (a.outboundId > b.outboundId) return -1;
				if (a.outboundId < b.outboundId) return 1;
				return 0;
			},

			handleSortChange({ column, prop, order }) {
			      if (!order) {
			        // 如果没有排序，则不进行任何操作
			        return;
			      }
			      this.tableData = this.tableData.slice().sort((a, b) => {
			        // 先根据number排序
			        if (order === 'ascending') {
			          if (a.outboundStatus != 2) return 1;
			          if (b.outboundStatus == 2) return -1;
			        } else {
			          if (a.outboundStatus == 2) return 1;
			          if (b.outboundStatus != 2) return -1;
			        }

			        // 再根据id排序
			        if (order === 'ascending') {
			          // 再根据编号排序
			          if (a.outboundId < b.outboundId) return -1;
			          if (a.outboundId > b.outboundId) return 1;
			        } else {
			          // 再根据编号排序
			          if (a.outboundId > b.outboundId) return -1;
			          if (a.outboundId < b.outboundId) return 1;
			        }

			        return 0; // 如果两个字段都相等，则返回0
			      });
			},
			shresetForm() {
				// 重置表单
				this.shform.result = 2;
				this.shform.comments = '';
				this.shopen = false; // 关闭弹出框
			},
			submitReview() {
				const outbound = this.outboundList.find(item => item.outboundId == this.ids);

				const data = {
					outboundId: this.ids[0],
					outboundStatus: this.shform.result, // 假设结果是 0 或 1
					reviewerReport: this.shform.comments,
					outboundType: outbound.outboundType,
					outboundCode: outbound.outboundCode,
					relatedNumber: outbound.relatedNumber,
				};

				updateStatus(data)
					.then(response => {
						console.log('审核提交成功:', response);
						this.getList();
					}).catch(error => {
						console.error('Fetching suppliers failed:', error);
					});
				this.shopen = false; // 关闭弹出框
			},
			// 审核
			sh() {
				this.shopen = true; // 打开弹出框

			},
			/** 查询出库管理列表 */
			getList() {
				this.loading = true;
				listOutbound(this.queryParams).then(response => {
					this.outboundList = response.rows;
					this.total = response.total;
					this.loading = false;
				});
			},
			// 取消按钮
			cancel() {
				this.open = false;
				this.reset();
			},
			// 表单重置
			reset() {
				this.form = {
					outboundId: null,
					outboundCode: null,
					outboundType: null,
					outboundStatus: null,
					warehouseCode: null,
					outboundTime: null,
					relatedNumber: null,
					relatedTarget: null,
					consignee: null,
					phone: null,
					reviewer: null,
					reviewerReport: null,
					reviewerTime: null,
					reviewerNumber: null,
					reviewerMoney: null,
					createBy: null,
					createTime: null,
					updateBy: null,
					updateTime: null,
					isDelete: null
				};
				this.resetForm("form");
			},
			/** 搜索按钮操作 */
			handleQuery() {
				this.queryParams.pageNum = 1;
				this.getList();
			},
			/** 重置按钮操作 */
			resetQuery() {
				this.resetForm("queryForm");
				this.handleQuery();
			},
			// 多选框选中数据
			handleSelectionChange(selection) {
				this.ids = selection.map(item => item.outboundId)
				this.single = selection.length !== 1
				this.multiple = !selection.length
				const isTrue = selection.map(item => item.outboundStatus);
				if (isTrue[0] == 2) {
					this.single = true;
				}
			},
			/** 新增按钮操作 */
			handleAdd() {
				// this.reset();
				// this.open = true;
				// this.title = "添加出库管理";

				this.$router.push({
					path: '/wms/outbound/outbound/outboundadd',
					query: {}
				}); // 使用绝对路径
			},
			/** 修改按钮操作 */
			handleUpdate(row) {
				// this.reset();
				// const outboundId = row.outboundId || this.ids
				// getOutbound(outboundId).then(response => {
				// 	this.form = response.data;
				// 	this.open = true;
				// 	this.title = "修改出库管理";
				// });

				this.$router.push({
					path: '/wms/outbound/outbound/outboundadd',
					query: {
						outboundId: row.outboundId
					}
				}); // 使用绝对路径
			},
			/** 提交按钮 */
			submitForm() {
				this.$refs["form"].validate(valid => {
					if (valid) {
						if (this.form.outboundId != null) {
							updateOutbound(this.form).then(response => {
								this.$modal.msgSuccess("修改成功");
								this.open = false;
								this.getList();
							});
						} else {
							addOutbound(this.form).then(response => {
								this.$modal.msgSuccess("新增成功");
								this.open = false;
								this.getList();
							});
						}
					}
				});
			},
			/** 删除按钮操作 */
			handleDelete(row) {
				const outboundIds = row.outboundId || this.ids;
				this.$modal.confirm('是否确认删除出库管理编号为"' + outboundIds + '"的数据项？').then(function() {
					return delOutbound(outboundIds);
				}).then(() => {
					this.getList();
					this.$modal.msgSuccess("删除成功");
				}).catch(() => {});
			},
			/** 导出按钮操作 */
			handleExport() {
				this.download('outbound/outbound/export', {
					...this.queryParams
				}, `outbound_${new Date().getTime()}.xlsx`)
			}
		}
	};
</script>
